<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="测评名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="测评名称"></el-input>
      </el-form-item>
      <el-form-item label="测评模板" prop="templateId">
        <el-select v-model="dataForm.templateId" placeholder="请选择测评模板">
          <el-option v-for="item in tempList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="周期" prop="value2">
        <el-date-picker v-model="dataForm.value2" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" date-format="YYYY/MM/DD ddd" time-format="A hh:mm:ss" @change="timeChange" />
      </el-form-item>
      <el-form-item label="年" prop="year">
        <el-select v-model="dataForm.year" placeholder="请选择年度" clearable>
          <el-option v-for="item in years" :key="item.label" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="月" prop="month">
        <el-select v-model="dataForm.month" placeholder="请选择月度" clearable>
          <el-option v-for="item in months" :key="item.label" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import { useTime } from "@/hooks/useTime";

const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);
const { year, month } = useTime();
const years = reactive<any>([]);
const tempList = ref();

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  name: "",
  year: year,
  month: month,
  status: 0,
  remark: "",
  templateId: undefined,
  startTime: "",
  endTime: "",
  value2: ""
}) as any;

for (let i = 1; i > -10; i--) {
  years.push({ label: year.value + i, value: year.value + i });
}
const months = reactive([
  { label: "1月", value: 1 },
  { label: "2月", value: 2 },
  { label: "3月", value: 3 },
  { label: "4月", value: 4 },
  { label: "5月", value: 5 },
  { label: "6月", value: 6 },
  { label: "7月", value: 7 },
  { label: "8月", value: 8 },
  { label: "9月", value: 9 },
  { label: "10月", value: 10 },
  { label: "11月", value: 11 },
  { label: "12月", value: 12 }
]);

const rules = ref({
  name: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  value2: [{ required: true, message: t("validate.required"), trigger: "change" }],
  templateId: [{ required: true, message: t("validate.required"), trigger: "change" }]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  baseService.get("/evaluation/clcevaluationtemplate/page", { limit: 500, page: 1 }).then((res) => {
    tempList.value = res.data.list;
  });

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/evaluation/clcevaluation/" + id).then((res) => {
    Object.assign(dataForm, res.data);
    dataForm.value2 = [dataForm.startTime, dataForm.endTime];
  });
};

const timeChange = (e: any) => {
  if (e) {
    dataForm.startTime = e[0];
    dataForm.endTime = e[1];
  } else {
    dataForm.startTime = "";
    dataForm.endTime = "";
  }
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    delete dataForm.value2;
    (!dataForm.id ? baseService.post : baseService.put)("/evaluation/clcevaluation", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
